// pages/address-add/address-add.js
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shifou_moren: false,
    region: ['省份、城市、县区'],
    shouhuo_name: '',
    shouhuo_tel: '',
    shouhuo_detailarea: '',
    is_default: '',
    checked:true,
    isDefault:'',
    edit:false,
    tjind:false
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bindIsDefault(){
    let isDefault = this.data.isDefault;
    if (isDefault == 1) {
      isDefault = 0;
    } else {
      isDefault = 1;
    }
    this.setData({
      isDefault: isDefault
    });
  },
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ checked: detail });
  },
  //添加地址
  Add_address() {
    let that = this;
    let checked = that.data.checked
    let isDefault = ''
    if(checked){
      isDefault = 1
    }else{
      isDefault = 0
    }
    if(!that.data.shouhuo_detailarea){
      common.showErrorToast('请填写地址');
      return
    }
    if(that.data.shouhuo_tel.length != 11){
      common.showErrorToast('请输入正确的手机号');
      return
    }
    if(!that.data.tjing){
      that.setData({

        tjing:true

      })
    let data = {
      shouhuo_name: that.data.shouhuo_name,
      shouhuo_tel: that.data.shouhuo_tel,
      shouhuo_area: that.data.region,
      shouhuo_detailarea: that.data.shouhuo_detailarea,
      is_default: isDefault
    }
    common.request(api.Add_address, data, 'post').then((res) => {
      if (res.status == 1) {
        common.showSuccessToast('添加成功');
        setTimeout(function () {
          that.setData({
            tjing:false
          })
          wx.navigateBack({ changed: true });
        }, 1500)
      }else{
        common.showSuccessToast(res.msg);
        that.setData({
          tjing:false
        })
      }
    })
  }
  },

  // 编辑地址
  edit_address(){
    let that = this;
    let checked = that.data.checked
    let isDefault = ''
    if(checked){
      isDefault = 1
    }else{
      isDefault = 0
    }
    if(!that.data.shouhuo_detailarea){
      common.showErrorToast('请填写地址');
      return
    }
    if(that.data.shouhuo_tel.length != 11){
      common.showErrorToast('请输入正确的手机号');
      return
    }
    if(!that.data.tjing){
      that.setData({

        tjing:true

      })

    
    let data = {
      addr_id:that.data.addr_id,
      shouhuo_name: that.data.shouhuo_name,
      shouhuo_tel: that.data.shouhuo_tel,
      shouhuo_area: that.data.region,
      shouhuo_detailarea: that.data.shouhuo_detailarea,
      is_default: isDefault
    }
    common.request(api.Edit_address, data, 'post').then((res) => {
      if (res.status == 1) {
        common.showSuccessToast('修改成功');
        setTimeout(function () {
          that.setData({
            tjing:false
          })
          wx.navigateBack({ changed: true });
        }, 1500)
      }else{
        common.showSuccessToast(res.msg);
        that.setData({
          tjing:false
        })
      }
    })
  }

  },
  //设置默认
  setMoren() {
    this.setData({
      shifou_moren: true,
      is_default: 1
    })
  },

  //取消默认
  quxiaoMoren() {
    this.setData({
      shifou_moren: false,
      is_default: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.addr_id){
      this.setData({
        addr_id:options.addr_id,
        edit:true
      })
      this.getinfo()
    }
  },


  //先获取到地址的信息
  getinfo() {
    let that = this;
    let addr_id = that.data.addr_id
    common.request(api.get_addrDetail, {
      addr_id: addr_id
    }, 'post').then((res) => {
      if (res.status == 1) {
        var checked
        if(res.oneaddr.is_default==1){
          checked = true
        }else{
          checked = false
        }
        that.setData({
          shouhuo_name: res.oneaddr.shouhuo_name,
          shouhuo_tel: res.oneaddr.shouhuo_tel,
          region: res.oneaddr.shouhuo_area,
          shouhuo_detailarea: res.oneaddr.shouhuo_detailarea,
          checked: checked,
        })

       
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})